package com.per.yyyFrame.common;

import com.per.yyyFrame.config.BizExceptional;
import com.per.yyyFrame.enums.RespCode;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/2/7
 * Time: 14:55
 * Description:链式校验小工具
 */
public class CheckParam {
    public static ThreadLocal<List> checkLocal=new ThreadLocal();
    static List errList=null;
    static Map paramMap=null;
    static String param=null;

    static volatile CheckParam checkParam=null;

    public CheckParam() {
    }

    public static CheckParam checkInit(Map map){
        if(checkParam==null){
            Class chPa=CheckParam.class;
            synchronized (chPa){
                if (checkParam==null){
                    checkParam=new CheckParam();
                }
            }
        }

        checkParam.errList=new ArrayList();
        checkParam.paramMap=map;
        return checkParam;
    }

    public CheckParam param(String param){
        this.param=param;
        return this;
    }

    public CheckParam checkCondition(String msg){
        if(!paramMap.containsKey(this.param)){
            this.errList.add(msg);
        }
        this.param=null;
        return this;
    }

    public ModelAndView verify(){
        checkLocal.remove();
        if(this.errList.size()>0){
            throw new BizExceptional(this.errList.get(0).toString());
        }
        return null;
    }
}
